/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package studio.raptor.ddal.jdbc;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.Collections;

import studio.raptor.ddal.core.engine.ProcessEngine;
import studio.raptor.ddal.jdbc.adapter.AbstractStatementAdapter;
import studio.raptor.ddal.jdbc.processor.StatementProcessor;

/**
 * 支持分片的静态语句对象.
 *
 * @author Jack, Sam
 */
public class RaptorStatement extends AbstractStatementAdapter {

  private final RaptorConnection raptorConnection;

  private final int resultSetType;

  private final int resultSetConcurrency;

  private final int resultSetHoldability;

  private ResultSet currentResultSet;

  RaptorStatement(final RaptorConnection raptorConnection) {
    this(raptorConnection, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
  }

  public RaptorStatement(final RaptorConnection raptorConnection, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) {
    super(Statement.class);
    this.raptorConnection = raptorConnection;
    this.resultSetType = resultSetType;
    this.resultSetConcurrency = resultSetConcurrency;
    this.resultSetHoldability = resultSetHoldability;
  }

  protected RaptorConnection getRaptorConnection() {
    return raptorConnection;
  }

  @Override
  public int getResultSetType() {
    return resultSetType;
  }

  @Override
  public int getResultSetConcurrency() {
    return resultSetConcurrency;
  }

  @Override
  public int getResultSetHoldability() {
    return resultSetHoldability;
  }

  protected void setCurrentResultSet(ResultSet currentResultSet) {
    this.currentResultSet = currentResultSet;
  }

  @Override
  public Connection getConnection() throws SQLException {
    return raptorConnection;
  }

  @Override
  public ResultSet executeQuery(final String sql) throws SQLException {
    return getStatementProcessor().executeQuery(sql);
  }

  @Override
  public int executeUpdate(final String sql) throws SQLException {
    return getStatementProcessor().executeUpdate(sql);
  }

  @Override
  public int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException {
    return getStatementProcessor().executeUpdate(sql, autoGeneratedKeys);
  }

  @Override
  public int executeUpdate(final String sql, final int[] columnIndexes) throws SQLException {
    return getStatementProcessor().executeUpdate(sql, columnIndexes);
  }

  @Override
  public int executeUpdate(final String sql, final String[] columnNames) throws SQLException {
    return getStatementProcessor().executeUpdate(sql, columnNames);
  }

  @Override
  public boolean execute(final String sql) throws SQLException {
    return getStatementProcessor().execute(sql);
  }

  @Override
  public boolean execute(final String sql, final int autoGeneratedKeys) throws SQLException {
    return getStatementProcessor().execute(sql, autoGeneratedKeys);
  }

  @Override
  public boolean execute(final String sql, final int[] columnIndexes) throws SQLException {
    return getStatementProcessor().execute(sql, columnIndexes);
  }

  @Override
  public boolean execute(final String sql, final String[] columnNames) throws SQLException {
    return getStatementProcessor().execute(sql, columnNames);
  }

  @Override
  public ResultSet getResultSet() throws SQLException {
    if (null == currentResultSet) {
      currentResultSet = new RaptorResultSet(getProcessEngine().getProcessContext().getMergedResult());
    }
    return currentResultSet;
  }

  @Override
  public Collection<? extends Statement> getRoutedStatements() throws SQLException {
    return Collections.emptyList();
  }

  private ProcessEngine getProcessEngine() {
    return this.raptorConnection.getProcessEngine();
  }

  private StatementProcessor getStatementProcessor() {
    return this.raptorConnection.getStatementProcessor();
  }

}
